package org.jeecg.modules.consumptionWater.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.chargeWater.service.IChargeWaterService;
import org.jeecg.modules.consumptionWater.entity.ConsumptionWater;
import org.jeecg.modules.consumptionWater.entity.WaterSelectDTO;
import org.jeecg.modules.consumptionWater.service.IConsumptionWaterService;
import org.jeecg.modules.feeWater.entity.FeeWater;
import org.jeecg.modules.feeWater.service.IFeeWaterService;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.math.MathContext;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @Description: 水能能耗
 * @Author: jeecg-boot
 * @Date:   2023-06-20
 * @Version: V1.0
 */
@Api(tags="水能能耗")
@RestController
@RequestMapping("/consumptionWater/consumptionWater")
@Slf4j
public class ConsumptionWaterController extends JeecgController<ConsumptionWater, IConsumptionWaterService> {
	@Autowired
	private IConsumptionWaterService consumptionWaterService;
	@Autowired
	private IFeeWaterService feeWaterService;
	@Autowired
	private IChargeWaterService chargeWaterService;
	
	/**
	 * 分页列表查询
	 *
	 * @param consumptionWater
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "水能能耗-分页列表查询")
	@ApiOperation(value="水能能耗-分页列表查询", notes="水能能耗-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(ConsumptionWater consumptionWater,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<ConsumptionWater> queryWrapper = QueryGenerator.initQueryWrapper(consumptionWater, req.getParameterMap());
		Page<ConsumptionWater> page = new Page<ConsumptionWater>(pageNo, pageSize);
		IPage<ConsumptionWater> pageList = consumptionWaterService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	 @PostMapping("/select")
	 public Result<?> selectByTimeAndDept(@RequestBody WaterSelectDTO selectDTO){
		 List<ConsumptionWater> list = consumptionWaterService.selectByTimeAndDept(selectDTO);
		 return Result.OK(list);
	 }

	 @GetMapping("/getAll")
	 public Result<?> getAll(){
		 Double all = consumptionWaterService.getAll();
		 return Result.OK(all);
	 }

	 @GetMapping("/getAllGroupByDept")
	 public Result<?> getAllGroupByDept(){
		 List<Map<String, Object>> list = consumptionWaterService.getAllGroupByDept();
		 return Result.OK(list);
	 }

	/**
	 *   添加
	 *
	 * @param consumptionWater
	 * @return
	 */
	@AutoLog(value = "水能能耗-添加")
	@ApiOperation(value="水能能耗-添加", notes="水能能耗-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody ConsumptionWater consumptionWater) {
		setDate(consumptionWater);
		FeeWater feeWater = getFeeWater(consumptionWater);
		feeWaterService.save(feeWater);
		consumptionWaterService.save(consumptionWater);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param consumptionWater
	 * @return
	 */
	@AutoLog(value = "水能能耗-编辑")
	@ApiOperation(value="水能能耗-编辑", notes="水能能耗-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody ConsumptionWater consumptionWater) {
		setDate(consumptionWater);
		FeeWater feeWater = getFeeWater(consumptionWater);
		feeWaterService.updateById(feeWater);
		consumptionWaterService.updateById(consumptionWater);
		return Result.OK("编辑成功!");
	}

	private static void setDate(ConsumptionWater consumptionWater) {
		if(StringUtils.isNotEmpty(consumptionWater.getMonth())&&StringUtils.isNotEmpty(consumptionWater.getYear())){
			String month=String.format("%2s",consumptionWater.getMonth()).replace(" ", "0");
			String day=String.format("%2s",consumptionWater.getDay()).replace(" ", "0");
			String date= consumptionWater.getYear()+month+day;
			consumptionWater.setDate(Integer.parseInt(date));
		}
	}

	@NotNull
	private FeeWater getFeeWater(ConsumptionWater consumptionWater) {
		FeeWater feeWater = new FeeWater();
		BigDecimal consumption = new BigDecimal(consumptionWater.getConsumption());
		Map<String, Object> map = chargeWaterService.getWaterProgramme();
		BigDecimal price = (BigDecimal) map.get("price");
		BigDecimal multiply = consumption.multiply(price, new MathContext(2));
		feeWater.setId(consumptionWater.getId()).setFee(multiply).setDept(consumptionWater.getDept())
				.setYear(consumptionWater.getYear()).setMonth(consumptionWater.getMonth()).setDate(consumptionWater.getDate());
		return feeWater;
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "水能能耗-通过id删除")
	@ApiOperation(value="水能能耗-通过id删除", notes="水能能耗-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		consumptionWaterService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "水能能耗-批量删除")
	@ApiOperation(value="水能能耗-批量删除", notes="水能能耗-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.consumptionWaterService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "水能能耗-通过id查询")
	@ApiOperation(value="水能能耗-通过id查询", notes="水能能耗-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		ConsumptionWater consumptionWater = consumptionWaterService.getById(id);
		if(consumptionWater==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(consumptionWater);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param consumptionWater
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, ConsumptionWater consumptionWater) {
        return super.exportXls(request, consumptionWater, ConsumptionWater.class, "水能能耗");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, ConsumptionWater.class);
    }

}
